# encoding: utf-8
# @File  : test04_edit_info.py
# @Author: wu shaofan
# @Date  :  2024/04/26
# @Desc : 修改个人信息
import json
import allure
from faker import Faker
import config
import conftest
from PO.page.page_login import PageLogin
from PO.page.page_user_info import PageUserinfo
from Utils.get_driver import GetDriver
from Utils.image_to_video import image_to_video
from Utils.record_log import Log


@allure.feature('3、个人中心模块')
class TestEditInfo:
    def setup_class(self):
        # 获取driver
        self.driver = GetDriver().get_driver(config.login_url)
        # 异常截图
        conftest.driver = self.driver
        # 获取登录页面对象
        self.login = PageLogin(self.driver)
        # 获取用户个人中心对象
        self.userinfo = PageUserinfo(self.driver)
        # TODO 创建一个Faker对象
        self.fake = Faker(locale='zh_CN')
        # 获取日志对象
        self.log = Log("修改个人信息").get_logger()

    def teardown_class(self):
        # 关闭driver
        GetDriver().close_driver()
        Log("修改个人信息").write_empty_line()

    @allure.story("1、编辑个人信息")
    def test_edit_info(self):
        with allure.step("步骤一：登录测试账号"):
            # 输入用户名, 密码, 验证码, 点击登录按钮
            self.login.page_input_username(config.testdata["用户名"])
            self.login.page_input_password(config.testdata["密码"])
            self.login.page_input_verify_code(config.testdata["验证码"])
            self.login.page_click_login_btn()

        with allure.step("步骤二：点击个人信息菜单"):
            self.userinfo.page_click_user_info_menu()

        with allure.step("步骤三：编辑头像"):
            self.userinfo.page_edit_head_pic()

        with allure.step("测试步骤三：进入iframe1框架"):
            self.userinfo.page_into_iframe()

        with allure.step("测试步骤四：上传图片"):
            image_path = config.BASE_DIR + r"/data/test04_head.jpg"
            self.userinfo.page_upload_image(image_path)

        with allure.step("测试步骤五：等待上传成功"):
            self.userinfo.page_wait_upload_success()

        with allure.step("测试步骤六：点击确定使用"):
            self.userinfo.page_click_confirm_use()
            # TODO 退出iframe框架
            self.driver.switch_to.default_content()

        with allure.step("测试步骤七：编辑昵称"):
            nickname = self.fake.name()
            self.userinfo.page_input_edit_nickname(nickname)

        with allure.step("测试步骤八：随即选择性别"):
            self.userinfo.page_input_edit_sex()

        with allure.step("测试步骤九：编辑生日"):
            birthday = self.fake.date(pattern="%Y-%m-%d", end_datetime=None)
            self.userinfo.page_input_edit_birthday(birthday)

        with allure.step("测试步骤十：点击确认保存"):
            self.userinfo.page_click_save_btn()

        with allure.step("断言：获取操作成功提示语"):
            success_tip = self.userinfo.page_get_success_tip()
            self.userinfo.base_get_allure_image()

        assert success_tip == "操作成功"
        data = {
            "用户信息": config.testdata,
            "修改内容": {
                "昵称": nickname,
                "生日": birthday},
            "是否修改成功": success_tip}
        self.log.info(f"测试数据：{data}")
        data = json.dumps(data, ensure_ascii=False, indent=4)
        allure.attach(data, "测试数据", allure.attachment_type.JSON)
